// PrivateChatDialog.cpp : implementation file
//

#include "stdafx.h"
#include "ChatConferencesClient.h"
#include "PrivateChatDialog.h"


// CPrivateChatDialog dialog

///<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
bool CPrivateChatDialog::initPrivateChat(
	CString srcUser, CString dstUser, CGlobalSocket *gSock)
{
	this ->srcUser = srcUser;
	this ->dstUser = dstUser;
	this ->gSock = gSock;
	this ->sClient = gSock ->getSockClient ();

	return true;
}

BOOL CPrivateChatDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	int err = WSAAsyncSelect(sClient,m_hWnd,WM_SOCKET,
				FD_READ|FD_ACCEPT|FD_CLOSE);
	if (err) {
		MessageBox(_T("Can't call WSAAsyncSelect"));
	}

	
	sReceivedMessage = _T("");
	sendType = 0;
	SetWindowTextW(_T("Private chat: " + srcUser + " <-> " + dstUser));
	//createPackage(105);  


	return true;
}


LRESULT CPrivateChatDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (message == WM_SOCKET){
		SockMsg(wParam,lParam);
	}
	return CDialog::WindowProc(message, wParam, lParam);
}
LRESULT CPrivateChatDialog::SockMsg(WPARAM wParam, LPARAM lParam)
{
	if (WSAGETSELECTERROR(lParam))
    {
		 // Display the error and close the socket
         closesocket(wParam);
    }
	switch(WSAGETSELECTEVENT(lParam))
    {
		
		case FD_READ:
			{
				if (!sendType){
					recv(sClient,(char*)&buffLength,sizeof(int),0);
					sendType=1;
				}
				else
				{
					char* buffer = new char[buffLength+1];
					recv(sClient,buffer,buffLength,0);
					buffer[buffLength]='\0';
					sendType=0;
					sReceivedMessage = buffer;
					delete buffer;
					vector<CString> res = CProcess::parseString(sReceivedMessage, '-');
					//MessageBox(sReceivedMessage);
//					choseAction(res);
				}
				break;
			}
		case FD_CLOSE:
			{
				closesocket(wParam);
			}
			break;
	}
	return 0;
}
///>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

IMPLEMENT_DYNAMIC(CPrivateChatDialog, CDialog)

CPrivateChatDialog::CPrivateChatDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CPrivateChatDialog::IDD, pParent)
{
	m_pParent = pParent;
}

CPrivateChatDialog::~CPrivateChatDialog()
{
}


void CPrivateChatDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CPrivateChatDialog, CDialog)
END_MESSAGE_MAP()


// CPrivateChatDialog message handlers
